//
//  GKHomeMoreController.swift
//  MySwiftObject
//
//  Created by wangws1990 on 2019/9/10.
//  Copyright © 2019 wangws1990. All rights reserved.
//

import UIKit

class GKHomeMoreController: BaseTableViewController {

    class func vcWithBookInfo(info:GKHomeInfo)-> UIViewController{
        let vc : GKHomeMoreController = GKHomeMoreController.init();
        vc.bookInfo = info;
        return vc;
    }
    var bookInfo:GKHomeInfo!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.showNavTitle(title: self.bookInfo.shortTitle);
        self.setupEmpty(scrollView: self.tableView);
        self.setupRefresh(scrollView: self.tableView, options: .Default);
    }
    override func refreshData(page: Int) {
        GKHomeNetManager.homeHot(rankId: self.bookInfo._id, sucesss: { (object) in
            let dic:NSDictionary = object as! NSDictionary;
            let rank :NSDictionary = dic .object(forKey: "ranking") as! NSDictionary
            self.bookInfo = GKHomeInfo.deserialize(from: rank);
            self.endRefresh(more: false);
        }) { (error) in
            self.endRefreshFailure()
        }
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1;
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.bookInfo.books!.count;
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension;
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell :GKClassifyTailCell = GKClassifyTailCell.cellForTableView(tableView: tableView, indexPath: indexPath)
        cell.model = self.bookInfo.books[indexPath.row];
        return cell;
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated:true);
        let model:GKBookModel = self.bookInfo.books[indexPath.row];
        GKJump.jumpToDetail(bookId: model.bookId ?? "");
    }

}
